%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     tcp 客户端测试
%%% @end
%%%-------------------------------------------------------------------
-module(net_tcp_test).

-export([
    test_game/0
    ,test_web/0
]).

-include("common.hrl").
-include("env.hrl").
-include("net.hrl").


test_game() ->
    erlang:spawn(fun
              () ->
                  Port = env_mgr:get(?ENV_NET_TCP_PORT),
                  {ok, Socket} = gen_tcp:connect("127.0.0.1", Port, [binary, {packet, 0}, {active, true}]),
                  gen_tcp:send(Socket, ?NET_CLIENT_GAME),
                  game_send(Socket),
                  game_recv(Socket, <<>>)
          end).

test_web() ->
    erlang:spawn(fun
              () ->
                  Port = env_mgr:get(?ENV_NET_TCP_PORT),
                  {ok, Socket} = gen_tcp:connect("127.0.0.1", Port, [binary, {packet, 0}, {active, false}]),
                  gen_tcp:send(Socket, ?NET_CLIENT_WEB),

                  Handler = env_mgr:get(?ENV_NET_WEB_HANDLER),
                  web_send(Socket, Handler, unixtime, []),

                  case gen_tcp:recv(Socket, 2, 3000) of
                      {ok, <<Len:16/big-integer-unsigned>>} ->
                          case gen_tcp:recv(Socket, Len, 5000) of
                              {ok, BinData} ->
                                  ?ECHO("test_web 服务端返回:~ts", [BinData]);
                              {error, Reason} ->
                                  ?ECHO("test_web 读取包体失败:~w", [Reason])
                          end;
                      Other ->
                          ?ECHO("test_web 读取包头失败[~p]", [Other])
                  end,
                  gen_tcp:close(Socket)
          end).


game_send(Socket) ->
    gen_tcp:send(Socket, [packet_1001(), packet_1001()]),
    gen_tcp:send(Socket, packet_1002()).

game_recv(Socket, Bin) ->
    receive
        {tcp, Socket, BinRecv} ->
            Bin2 = work(Socket, <<Bin/binary, BinRecv/binary>>),
            game_recv(Socket, Bin2);
        {tcp_closed, Socket} ->
            ?ECHO("test_game 服务端断开"),
            gen_tcp:close(Socket);
        {inet_reply, _Socket, ok} ->
            game_recv(Socket, Bin);
        _Other ->
            ?ECHO("test_game 接收未知消息:~p", [_Other]),
            game_recv(Socket, Bin)
    end.


web_send(Socket, Mod, Fun, Args) ->
    Time = util_time:unixtime(),
    Key = env_mgr:get(?ENV_NET_WEB_KEY),

    Sign = util_encrypt:md5(<<?BS(Args), "_", ?B(Time), "_", ?B(Key)>>),

    BinData = util_type:term_to_bitstring({Mod, Fun, Args, Time, Sign}),
    LenBin	= byte_size(BinData),
    gen_tcp:send(Socket, <<LenBin:16/big-integer-unsigned, BinData/binary>>).


work(Socket, <<Len:16/big-integer-unsigned, BinData:Len/binary, BinAcc/binary>>) ->
    <<PacketId:16/big-integer-unsigned, BinData2/binary>> = BinData,
    routing(PacketId, BinData2),
    work(Socket, BinAcc);
work(_Socket, BinAcc) ->
    BinAcc.

routing(1001, <<Len:16/big-integer-unsigned, Name:Len/binary, Age:8/big-integer-unsigned>>) ->
    ?ECHO("client routing 1001 name:~ts,age:~w", [Name, Age]);
routing(1002, <<Gold:16/big-integer-unsigned, Diamond:16/big-integer-unsigned>>) ->
    ?ECHO("client routing 1002 Gold:~w,Diamond:~w", [Gold, Diamond]).


packet_1001() ->
    Name = <<"你 mirahs 好"/utf8>>,
    Age = 18,
    Len = erlang:size(Name),
    packet_msg(1001, <<Len:16/big-integer-unsigned, Name/binary, Age:8/big-integer-unsigned>>).

packet_1002() ->
    Gold = 111,
    Diamond = 222,
    packet_msg(1002, <<Gold:16/big-integer-unsigned, Diamond:16/big-integer-unsigned>>).

packet_msg(MsgId, BinData) ->
    LenBin	= byte_size(BinData),
    Len		= LenBin,
    <<Len:16/big-integer-unsigned, MsgId:16/big-integer-unsigned, BinData/binary>>.
